<?php declare(strict_types=1);
/**
 * @author      guosir
 * @copyright   2023-2025 owner
 **/

namespace Srv\Apps\Apis\Skit;

use Srv\Apps\Apis\Log\Ctl\LogCtl;
use Srv\Apps\Apis\Skit\Ctl\SkitSeriesCtl;
use Srv\Apps\Proto\DTO\Skit\SkitSeriesAddBatchDTO;
use Srv\Apps\Proto\DTO\Skit\SkitSeriesAddDTO;
use Srv\Apps\Proto\DTO\Skit\SkitSeriesEditDTO;
use Srv\Apps\Proto\DTO\Skit\SkitSeriesExtEditDTO;
use Srv\Apps\Proto\DTO\Skit\SkitSeriesListDTO;
use Srv\Apps\Proto\Enum\ErrCodeEnum;
use Srv\Apps\Proto\Enum\LangKeyEnum;
use Srv\Apps\Proto\Enum\Log\LogResultEnum;
use Srv\Apps\Proto\VO\Skit\SkitSeriesListVO;
use Srv\Libs\ConfModel\RouteConfData;
use Srv\Libs\Frame\ActionAbstract;
use Srv\Libs\Frame\DTOAbstract;
use Srv\Libs\Frame\Lang;
use Srv\Libs\Frame\Page;
use Srv\Libs\Frame\ResponseData;
use Srv\Libs\Frame\VOAbstract;

final class SkitSeriesAct extends ActionAbstract
{
    /**
     * @url /skit/series/list
     * @auth skitSeriesList
     * @req Srv.Apps.Proto.DTO.Skit.SkitSeriesListDTO
     * @rsp Srv.Apps.Proto.VO.Skit.SkitSeriesListVO
     * 剧集信息列表
     */
    public function getSkitSeriesList(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        if(!($DTOClass instanceof SkitSeriesListDTO) || !($VOClass instanceof SkitSeriesListVO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $Page                   = new Page();
        $currPage               = max(1, $DTOClass->getCurrPage());
        $pageSize               = $DTOClass->getPageSize() < 1 ? 12 : $DTOClass->getPageSize();
        $Page->init(0, $pageSize, $currPage);
        $whereData              = $RouteConfData->getParamsResData($DTOClass);
        $VOClass                = SkitSeriesCtl::tag()->getSkitSeriesList($Page, $whereData);
        return ResponseData::rspSuccAuto('', $VOClass, []);
    }

    /**
     * @url /skit/series/add
     * @auth skitSeriesAdd
     * @req Srv.Apps.Proto.DTO.Skit.SkitSeriesAddDTO
     * 剧集信息新增
     */
    public function skitSeriesAdd(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass);
        if(!($DTOClass instanceof SkitSeriesAddDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $setData        = $RouteConfData->getParamsResData($DTOClass);
        $LogCtl         = LogCtl::tag();
        $logData        = $setData;
        $skssId         = SkitSeriesCtl::tag()->skitSeriesAdd($setData);
        if($skssId < 1) {
            $errMsg     = '剧集信息新增失败';
            $LogCtl->logAdd(LogCtl::SKIT_SERIES_ADD, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg         = '剧集信息新增成功';
        $LogCtl->logAdd(LogCtl::SKIT_SERIES_ADD, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', null, []);
    }

    /**
     * @url /skit/series/edit
     * @auth skitSeriesEdit
     * @req Srv.Apps.Proto.DTO.Skit.SkitSeriesEditDTO
     * 剧集信息编辑
     */
    public function skitSeriesEdit(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass);
        if(!($DTOClass instanceof SkitSeriesEditDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $skssId         = intval($DTOClass->getSkssId());
        $setData        = $RouteConfData->getParamsResData($DTOClass);
        $LogCtl         = LogCtl::tag();
        $SkitSeriesCtl  = SkitSeriesCtl::tag();
        $seriesSrc      = $SkitSeriesCtl->getSkitSeriesSrc($skssId);
        if(!isset($seriesSrc['skss_id'])) {
            $logData    = $setData;
            $errMsg     = '剧集信息不存在';
            $LogCtl->logAdd(LogCtl::SKIT_SERIES_EDIT, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $skioIdOld      = intval($seriesSrc['skio_id']);
        $result         = $SkitSeriesCtl->skitSeriesEdit($skssId, $setData, $skioIdOld);
        $logData        = ['new' => $setData, 'old' => $seriesSrc];
        if(!$result){
            $errMsg     = '剧集信息编辑失败';
            $LogCtl->logAdd(LogCtl::SKIT_SERIES_EDIT, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg         = '剧集信息编辑成功';
        $LogCtl->logAdd(LogCtl::SKIT_SERIES_EDIT, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', null, []);
    }

    /**
     * @url /skit/series/add/batch
     * @auth skitSeriesAddBatch
     * @req Srv.Apps.Proto.DTO.Skit.SkitSeriesAddBatchDTO
     * 剧集信息新增-批量
     */
    public function skitSeriesAddBatch(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass);
        if(!($DTOClass instanceof SkitSeriesAddBatchDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $setData        = $RouteConfData->getParamsResData($DTOClass);
        $errSeries      = [];
        $LogCtl         = LogCtl::tag();
        $logData        = $setData;
        $result         = SkitSeriesCtl::tag()->skitSeriesAddBatch($setData, $errSeries);
        if(!$result) {
            $errMsg     = '剧集信息新增-批量失败';
            $LogCtl->logAdd(LogCtl::SKIT_SERIES_ADD_BATCH, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $logData        = ['setData' => $setData, 'errSeries' => $errSeries];
        $errMsg         = '剧集信息新增-批量成功';
        $LogCtl->logAdd(LogCtl::SKIT_SERIES_ADD_BATCH, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        if(count($errSeries) > 0){
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SKIT_SERIES_ADD_BATCH_ERROR, Lang::getString(LangKeyEnum::_LANG_SKIT_SERIES_ADD_BATCH_ERROR, ['SERIES_STR' => implode(',', $errSeries)]));
        }
        return ResponseData::rspSuccAuto('', null, []);
    }

    /**
     * @url /skit/series/ext/edit
     * @auth skitSeriesExtEdit
     * @req Srv.Apps.Proto.DTO.Skit.SkitSeriesExtEditDTO
     * 剧集扩展数据编辑
     */
    public function skitSeriesExtEdit(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        $setData        = $RouteConfData->getParamsResData($DTOClass);
        $LogCtl         = LogCtl::tag();
        $SkitSeriesCtl  = SkitSeriesCtl::tag();
        $oldData        = [];
        $newData        = $setData[SkitSeriesExtEditDTO::_extItemList];
        if(!$SkitSeriesCtl->checkExtData($newData, $oldData)){
            $logData    = $setData;
            $errMsg     = '数据检查错误';
            $LogCtl->logAdd(LogCtl::SKIT_SERIES_EXT_EDIT, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_ERROR, Lang::getString(LangKeyEnum::_LANG_ERROR));
        }
        $logData        = ['new' => $setData, 'old' => $oldData];
        $skioIdList     = array_map('intval', array_unique(array_column($oldData, 'skioId')));
        if(!$SkitSeriesCtl->skitSeriesExtEdit($newData, $skioIdList)){
            $errMsg     = '剧集扩展数据编辑失败';
            $LogCtl->logAdd(LogCtl::SKIT_SERIES_EXT_EDIT, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg         = '剧集扩展数据编辑成功';
        $LogCtl->logAdd(LogCtl::SKIT_SERIES_EXT_EDIT, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', null, []);
    }
}